package com.cpj.bsyyzx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpj.bsyyzx.entity.FileNum;
import com.cpj.bsyyzx.entity.HealthFile;
import com.cpj.bsyyzx.entity.Register;
import com.cpj.bsyyzx.entity.User;
import com.cpj.bsyyzx.mapper.FileNumMapper;
import com.cpj.bsyyzx.service.IFileNumService;
import com.cpj.bsyyzx.service.IHealthFileService;
import com.cpj.bsyyzx.util.CommonResult;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 五个臭皮匠
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/healthFile")
public class HealthFileController {

    @Resource
    IHealthFileService healthFileService;
    @Resource
    IFileNumService fileNumService;

    @Resource
    FileNumMapper fileNumMapper;

//    @RequestMapping("/list")
//    @ResponseBody
//    CommonResult pagelist(Page page){
//        QueryWrapper<HealthFile> queryWrapper = new QueryWrapper<>();
////        log.debug("分页查询");
//        IPage<HealthFile> rpage=healthFileService.selectPageVo(page,queryWrapper);
//        return CommonResult.success(rpage);
//    }
    @GetMapping("/list")
    @ResponseBody
    CommonResult  page(Page page, HealthFile healthFile){

        QueryWrapper queryWrapper = new QueryWrapper();
        if(healthFile!= null && StringUtils.isNotEmpty(healthFile.getUserName())){
            queryWrapper.like("user_name",healthFile.getUserName());
        }
        return CommonResult.success(healthFileService.page(page,queryWrapper));
    }


    @RequestMapping("add")
    @ResponseBody
    CommonResult save(HealthFile healthFile) {
        FileNum fileNum = fileNumMapper.selectFileByHealth();
        Integer list = fileNum.getMaxnum();
        list+=1;
        fileNum.setMaxnum(list);
        fileNumService.saveOrUpdate(fileNum);
        healthFile.setRecordNum("Health"+list);

        if(healthFileService.add(healthFile))
            return CommonResult.success("ok");
        else
            return CommonResult.failed("姓名已经存在");
    }
    @RequestMapping("/update")
    @ResponseBody
    CommonResult update(HealthFile healthFile) {
        return CommonResult.success(healthFileService.updateById(healthFile));
    }



    @RequestMapping("toedit")
    @ResponseBody
    CommonResult toedit(Integer fileId) {
        return CommonResult.success(healthFileService.getById(fileId));
    }
    @RequestMapping("delete/{fileId}")
    @ResponseBody
    CommonResult delete(@PathVariable Integer fileId) {
//        log.debug("bbb根据主键删除:" + id);
        boolean success = healthFileService.removeById(fileId);
        return CommonResult.success(success);
    }
    @RequestMapping("/batchdel")
    @ResponseBody
    CommonResult batchdel(Integer[] id) {
        return CommonResult.success(healthFileService.removeByIds(Arrays.asList(id)));
    }

}
